package com.leyou.item.web;

import com.leyou.commons.dto.PageDTO;
import com.leyou.item.dto.SkuDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.dto.SpuDTO;
import com.leyou.item.dto.SpuDetailDTO;
import com.leyou.item.entity.Sku;
import com.leyou.item.entity.SpuDetail;
import com.leyou.item.service.SkuService;
import com.leyou.item.service.SpecParamService;
import com.leyou.item.service.SpuDetailService;
import com.leyou.item.service.SpuService;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    private SpuService spuService;
    @Autowired
    private SkuService skuService;
    @Autowired
    private SpuDetailService spuDetailService;
    @Autowired
    private SpecParamService specParamService;
    /**todo：
     * 商品列表分页查询
     * @param page
     * @param rows
     * @param saleable
     * @param categoryId
     * @param brandId
     * @param id
     * @return
     */
    @GetMapping("/spu/page")
    public ResponseEntity<PageDTO<SpuDTO>> querySpuByPage(    @RequestParam(value = "page", defaultValue = "1") Integer page,
                                                              @RequestParam(value = "rows", defaultValue = "5") Integer rows,
                                                              @RequestParam(value = "saleable", required = false) Boolean saleable,
                                                              @RequestParam(value = "categoryId", required = false) Integer categoryId,
                                                              @RequestParam(value = "brandId", required = false) Integer brandId,
                                                              @RequestParam(value = "id", required = false) Integer id){
       return  ResponseEntity.ok(spuService.querySpuByPage(page,rows,saleable,categoryId,brandId,id));
    }
    @PutMapping("/saleable")
    //修改商品是否上架状态
    //todo:
    public  ResponseEntity<Void> updateSpuSaleable(@RequestParam("id") long id,
                                                   @RequestParam("saleable") Boolean saleable){
        spuService.updateSpuSaleable(id,saleable);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * todo：
     * 新增商品
     * @param spuDTO
     * @return
     */
    @PostMapping("/spu")
    public ResponseEntity<Void> SaveGoods(@RequestBody SpuDTO spuDTO){
        spuService.SaveGoods(spuDTO);
    return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**todo:
     * 根据商品id查询商品 并传入规格参数
     * @return
     */
    @GetMapping("/{id}")
    public ResponseEntity<SpuDTO> queryGoodsByid(@PathVariable("id") Integer spuId){
        return ResponseEntity.ok(spuService.queryGoodsByid(spuId));
    }

    /**todo：
     * 更新商品信息
     * @param spuDTO
     * @return
     */
    @PutMapping("/spu")
    public ResponseEntity<Void> updateGoods(@RequestBody SpuDTO spuDTO){
        spuService.updateGoods(spuDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**todo
     * 根据sku的id集合查询sku集合
     * @param ids
     * @return
     */
    @GetMapping("/sku/list")
    public ResponseEntity<List<SkuDTO>> querySkuByIds(@RequestParam("ids") List<Long> ids){
        return ResponseEntity.ok(SkuDTO.convertEntityList(skuService.listByIds(ids)));
    }

    /**todo
     * 根据商品id查询sku的集合
     * @param id
     * @return
     */
    @GetMapping("/sku/of/spu")
    public ResponseEntity<List<SkuDTO>> querySkuBySpuId(@RequestParam("id") Integer id){
      return   ResponseEntity.ok(SkuDTO.convertEntityList(skuService.query().eq("spu_id",id).list()));
    }

    /**todo
     * 根据id查询商品详情
     * @param id
     * @return
     */
    @GetMapping("/spu/detail")
    public  ResponseEntity<SpuDetailDTO> querySpudetailBySpuId(@RequestParam("id")Integer id){
        return ResponseEntity.ok(new SpuDetailDTO(spuDetailService.getById(id)));
    }

    /**todo
     * 根据spuId查询spu的所有规格参数值
     * @return
     */
    @GetMapping("/spec/value")
    public ResponseEntity<List<SpecParamDTO>> querySpecvalues(@RequestParam("id") Integer spuId,
                                                              @RequestParam(value = "searching",required = false) Boolean searching
                                                              ){
        return ResponseEntity.ok(specParamService.querySpecvalues(spuId,searching));
    }

    /**
     * todo
     * 根据ID查询商品
     * @return
     */
    @GetMapping("/spu/{id}")
    public ResponseEntity<SpuDTO> querySpuById(@PathVariable("id") Integer id){
        return ResponseEntity.ok(new SpuDTO(spuService.getById(id)));
    }

    /**
     * 扣减库存
     * @param cartMap 商品集合
     */
    @PutMapping("/stock/minus")
    public ResponseEntity<Void> deductStock(@RequestBody Map<Long, Integer> cartMap){
        skuService.deductStock(cartMap);
        return ResponseEntity.noContent().build();
    }
    /**
     * 加库存
     * @param cartMap 商品id及数量的map
     */
    @PutMapping("/stock/plus")
    public ResponseEntity<Void> addStock(@RequestBody Map<Long, Integer> cartMap){
        skuService.addStock(cartMap);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }
}
